import time

import datetime


class MyQueue:

    def __init__(self):
        self.s = []

    def push(self, x: tuple) -> None:
        self.s.append(x)

    def pop(self) -> tuple:
        return self.s.pop(0)

    def empty(self) -> bool:
        return not bool(self.s)
    def insert(self, x:int ,y:tuple):
        self.s.insert(x,y)

    def get(self):
        return self.s


# myq = MyQueue()
#
# myq.push((uid, username))
#
# print(myq.pop())

import redis

class RedisQueue:

    def __init__(self,key,**redis_kwargs):

        self.__db = redis.Redis(**redis_kwargs)

        self.key = key

    def put(self,item):

        self.__db.rpush(self.key,item)

    def pop(self):

        item = self.__db.lpop(self.key)

        return item

q = RedisQueue('redisqueue')

# [q.put(i) for i in range(100)]
#
# print(q.pop())
#
# for i in range(100):
#
#     print(q.pop())


import threading
# 异步消费
async def dojob():

    q = RedisQueue('redisqueue')

    while 1:

        result = q.pop()

        if not result:

            break

        print(result)


# 启动多线程
# for index in range(3):
#
#     thread = threading.Thread(target=dojob)
#
#     thread.start()




# 线程池
from concurrent.futures import ThreadPoolExecutor


with ThreadPoolExecutor(max_workers =5) as t:

    [t.submit(dojob) for i in range(2)]



# def test(value1, value2=None):
#      print("%s个线程 执行了第%s和%s的参数"%(threading.current_thread().name, value1, value2))
#      time.sleep(2)
#      return 'finished'
#
# def test_result(future):
#      print(future.result())
#
# if __name__ == "__main__":
#
#      threadPool = ThreadPoolExecutor(max_workers=4, thread_name_prefix="这里是第")
#      for i in range(0,10):
#          future = threadPool.submit(test, i,i+1)
#
#      threadPool.shutdown(wait=True)


# import asyncio
#
# loop =asyncio.get_event_loop()
#
# res = loop.run_until_complete(dojob())
#
# loop.close()